package com.southpark.system.managers
{
	import com.southpark.framework.managers.ErrorManager;
	import com.southpark.system.interfaces.managers.IApplicationManager;
	import com.southpark.system.interfaces.managers.IPageManager;
	import com.southpark.system.interfaces.view.IViewController;
	import com.southpark.system.interfaces.view.IViewUI;
	
	import flash.utils.Dictionary;
	
	import mx.containers.ViewStack;
	import mx.core.Container;
	

	
	public class PageManager implements IPageManager
	{	
		
		public function PageManager(applicationManager:IApplicationManager)
		{
			this.applicationManager = applicationManager;
			createPageDictionary();
		}

		
		/**
		 * 
		 */
		private var applicationManager:IApplicationManager;
		private var pageDictionary:Dictionary;
		
		/**
		 * 
		 */
		private function createPageDictionary():void
		{
			pageDictionary = new Dictionary();
			
			for each (var item:Array in applicationManager.pageMap)
			{
				definePage(item[0],item[1]);
			};
		} 
		
		private function definePage(pageInterface:Class,pageClass:Class):void
		{	
		 	if (pageDictionary[pageInterface])
		 	{
		 		ErrorManager.throwError(ErrorManager.DUPLICATE_DEFINITION,this);
		 		return;
		 	};
		 
		 	pageDictionary[pageInterface] = pageClass;
		}
		
		
		
		private function get pageLayer():ViewStack
		{
			return applicationManager.background.pageLayer;
		}
		
		/**
		 * 
		 */
		
		private var _selectedPageInterface:Class;
		[Bindable]public function get selectedPageInterface():Class
		{
			return _selectedPageInterface;
		}
		public function set selectedPageInterface(value:Class):void
		{
		}
		
		public function get selectedPageViewUI():IViewUI
		{
			return IViewUI(pageDictionary[selectedPageInterface]);
		}
		
		public function isActivePage(viewController:IViewController):Boolean
		{
			return (viewController == selectedPageViewUI.viewController);
		}
		
		public function addPage(pageInterface:Class):IViewController
		{
			if (!pageDictionary[pageInterface]) 
			{
				applicationManager.infoManager.showApplicationError("Page " + Class(pageInterface).toString() + " is not included");
				return null;
			};
			
			if (selectedPageInterface) selectedPageViewUI.viewController.onHide();
			_selectedPageInterface = pageInterface;
			
			var page:Container;
			
			if (pageDictionary[selectedPageInterface] is IViewUI)
		 	{
		 		page = pageDictionary[selectedPageInterface] as Container;
		 		IViewUI(page).viewController.onShow();
		 	}
		 	else if (pageDictionary[selectedPageInterface] is Class)
		 	{
		 		page = new pageDictionary[selectedPageInterface]();
		 		
		 		page.percentWidth = 100;
		 		page.percentHeight = 100;
		 		
		 		pageLayer.addChild(page);
		 		
		 		IViewUI(page).viewController.onCreated();
		 		
		 		pageDictionary[selectedPageInterface] = page;
		 	};
		 	
		 	pageLayer.selectedChild = page;

			applicationManager.deepLinkingManager.updateDeepLinking();
		
			return IViewUI(page).viewController;
		}
		
	}
}